/**
 * Live GPS Tracking Application
 * Loads tiles from a network source
 *
 * Copyright 2009, Ivar Clemens
 */

package nl.ivarclemens.gpstrack.tile;

import java.io.*;
import javax.microedition.io.*;

/**
 *
 * @author ivar
 */
public class NetworkSource {

    /**
     * Downloads tile and returns raw data (for caching)
     */
    public byte[] loadTile(int x, int y, int zoom) throws IOException
    {
        String url;
        
        url = "http://tile.openstreetmap.org/" + zoom + "/" + x + "/" + y + ".png";

        InputStream is = (InputStream) Connector.openInputStream(url);
        byte data[];

        try {
            ByteArrayOutputStream bs = new ByteArrayOutputStream();

            int ch;

            while((ch = is.read()) != -1)
                bs.write(ch);

            data = bs.toByteArray();
        } finally {
            if(is != null)
                is.close();
        }

        return data;
    }
}
